package com.sdhs.web.controller.system;

import com.github.pagehelper.PageInfo;
import com.sdhs.common.annotation.Log;
import com.sdhs.common.core.controller.BaseController;
import com.sdhs.common.core.domain.AjaxResult;
import com.sdhs.common.core.domain.R;
import com.sdhs.common.core.page.TableDataInfo;
import com.sdhs.common.enums.BusinessType;
import com.sdhs.common.utils.TextUtil;
import com.sdhs.system.domain.SysNotice;
import com.sdhs.system.service.ISysNoticeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 公告 信息操作处理
 * 
 * @author engineering
 */
@RestController
@RequestMapping("/system/notice")
public class SysNoticeController extends BaseController
{
    @Autowired
    private ISysNoticeService noticeService;

    /**
     * 获取通知公告列表
     */
    @GetMapping("/list")
    public TableDataInfo list(SysNotice notice)
    {
        startPage();
        List<SysNotice> list = noticeService.selectNoticeList(notice);
        TableDataInfo dataTable = getDataTable(list);
        dataTable.setCurrent(new PageInfo(list).getPageNum());
        dataTable.setPages(new PageInfo(list).getPages());
        return dataTable;
//        return getDataTable(list);
    }

    /**
     * 根据通知公告编号获取详细信息
     */
    @GetMapping(value = "/{noticeId}")
    public AjaxResult getInfo(@PathVariable Long noticeId)
    {
        return success(noticeService.selectNoticeById(noticeId));
    }

    /**
     * 新增通知公告
     */
    @Log(title = "通知公告", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@Validated @RequestBody SysNotice notice)
    {
        notice.setCreateBy(getUsername());
        return toAjax(noticeService.insertNotice(notice));
    }

    /**
     * 修改通知公告
     */
    @Log(title = "通知公告", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@Validated @RequestBody SysNotice notice)
    {
        notice.setUpdateBy(getUsername());
        return toAjax(noticeService.updateNotice(notice));
    }

    /**
     * 删除通知公告
     */
    @Log(title = "通知公告", businessType = BusinessType.DELETE)
    @DeleteMapping("/{noticeIds}")
    public AjaxResult remove(@PathVariable Long[] noticeIds)
    {
        return toAjax(noticeService.deleteNoticeByIds(noticeIds));
    }

    /**
     * 小铃铛查询未读通知
     */
    @Log(title = "未读列表", businessType = BusinessType.OTHER)
    @PostMapping("/listUnread")
    public R listUnread()
    {
        return R.ok(noticeService.listUnRead());
    }

    /**
     * 小铃铛查询未读通知条数
     */
    @Log(title = "未读列表条数", businessType = BusinessType.OTHER)
    @PostMapping("/listUnreadSize")
    public R listUnreadSize()
    {
        return R.ok(noticeService.listUnReadSize());
    }

    /**
     * 将通知置为已读（单条）
     */
    @Log(title = "已读通知", businessType = BusinessType.OTHER)
    @PostMapping("/readNotice")
    public R readNotice(@RequestBody SysNotice sysNotice )
    {
        Long noticeId = sysNotice.getNoticeId();
        return R.ok(noticeService.readNotice(noticeId));
    }

    /**
     * 将通知置为已读（批量）
     */
    @Log(title = "已读通知", businessType = BusinessType.OTHER)
    @PostMapping("/readBatchNotice")
    public R readBatchNotice(@RequestBody SysNotice sysNotice )
    {
        String readIds = sysNotice.getReadIds();
        return R.ok(noticeService.readBatchNotice(readIds));
    }

    /**
     * 全部已读
     */
    @Log(title = "全部已读", businessType = BusinessType.OTHER)
    @PostMapping("/readAllNotice")
    public R readAllNotice( )
    {
        //查询所有未读id
        String readIds = this.noticeService.listUnReadId();
        if(TextUtil.isNotNull(readIds)){
            return R.ok(noticeService.readBatchNotice(readIds));
        }
         return    R.ok();


    }
}
